package api

import (
	"getaway/basic/config"
	driver "getaway/basic/proto/driverproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"github.com/gin-gonic/gin"
)

// AddVehicle
// @Summary      添加车辆
// @Description  添加车辆
// @Tags         车辆模块
// @Accept       json
// @Produce      json
// @Param        vehicle   body    request.AddVehicle    true  "车辆参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addVehicle [post]
func AddVehicle(c *gin.Context) {
	var req request.AddVehicle
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	add, err := config.DriverClient.VehicleAdd(c, &driver.VehicleAddReq{
		DeviceId:            req.DeviceId,
		VehicleFilesId:      req.VehicleFilesId,
		CityId:              req.CityId,
		LevelId:             req.LevelId,
		ModelId:             req.ModelId,
		LicensePlate:        req.LicensePlate,
		IsInstalledCarBrain: req.IsInstalledCarBrain,
		CarBrainBrand:       req.CarBrainBrand,
		CarBrainNumber:      req.CarBrainNumber,
		LargeScreenBrand:    req.LargeScreenBrand,
		LargeScreenNumber:   req.LargeScreenNumber,
		VinCode:             req.VinCode,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}

	// 记录添加车辆行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "添加车辆", map[string]interface{}{
		"licensePlate": req.LicensePlate,
		"deviceId":     req.DeviceId,
		"success":      err == nil,
	})

	response.Success(c, "添加车辆成功", add.VehicleId)
}

// AddVehicleFiles
// @Summary      车辆证件添加
// @Description  车辆证件添加
// @Tags         车辆模块
// @Accept       json
// @Produce      json
// @Param        vehicle   body    request.AddVehicleFiles    true  "车辆参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addVehicleFiles [post]
func AddVehicleFiles(c *gin.Context) {
	var req request.AddVehicleFiles
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	vehicleFiles, err := config.DriverClient.VehicleFilesAdd(c, &driver.VehicleFilesAddReq{
		VehicleInvoicePath:          req.VehicleInvoicePath,
		CertificatePath:             req.CertificatePath,
		DrivingProofPath:            req.DrivingProofPath,
		RegistrationCertificatePath: req.RegistrationCertificatePath,
		TaxProofPath:                req.TaxProofPath,
		VehicleTransportCertPath:    req.VehicleTransportCertPath,
		OtherOnePath:                req.OtherOnePath,
		OtherTwoPath:                req.OtherTwoPath,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}

	// 记录添加车辆证件图片行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "添加车辆证件图片", map[string]interface{}{
		"success": err == nil,
	})

	response.Success(c, "添加车辆证件图片成功", vehicleFiles.VehicleFilesId)
}

// AddRideHailing
// @Summary      网约车添加
// @Description  网约车添加
// @Tags         车辆模块
// @Accept       json
// @Produce      json
// @Param        vehicle   body    request.AddRideHailing    true  "车辆参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/addRideHailing [post]
func AddRideHailing(c *gin.Context) {
	var req request.AddRideHailing
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	rideHailing, err := config.DriverClient.RideHailingAdd(c, &driver.RideHailingAddReq{
		CompanyIdentifier:          req.CompanyIdentifier,
		VehicleBrand:               req.VehicleBrand,
		VehicleType:                req.VehicleType,
		VehicleOwner:               req.VehicleOwner,
		EngineOrMotorNumber:        req.EngineOrMotorNumber,
		RegistrationDate:           req.RegistrationDate,
		FuelType:                   req.FuelTypes,
		EngineDisplacement:         float32(req.EngineDisplacement),
		VehiclePhotoFileNumber:     req.VehiclePhotoFileNumber,
		TransportationPermitNumber: req.TransportationPermitNumber,
		PermitIssuingAuthority:     req.PermitIssuingAuthority,
		OperationArea:              req.OperationArea,
		PermitValidFrom:            req.PermitValidFrom,
		PermitValidTo:              req.PermitValidTo,
		FirstRegistrationDate:      req.FirstRegistrationDate,
		MaintenanceStatus:          req.MaintenanceStatus,
		NextInspectionDate:         req.NextInspectionDate,
		AnnualInspectionStatus:     req.AnnualInspectionStatus,
		InvoicePrinterSerialNumber: req.InvoicePrinterSerialNumber,
		GpsBrand:                   req.GpsBrand,
		GpsModel:                   req.GpsModel,
		GpsImei:                    req.GpsImei,
		GpsInstallationDate:        req.GpsInstallationDate,
		DevicePurchaseDate:         req.DevicePurchaseDate,
		ServiceType:                req.ServiceType,
		FreightTypeCode:            req.FreightTypeCode,
		DeviceId:                   int64(req.DriverId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}

	// 记录添加车辆扩展信息行为
	userId, _ := c.Get("userId")
	middleware.RecordClickBehavior(c, userId, "网约车添加", map[string]interface{}{
		"DeviceId":     req.DriverId,
		"vehicleBrand": req.VehicleBrand,
		"success":      err == nil,
	})

	response.Success(c, "网约车添加成功", rideHailing.RideHailingId)

}

// GetVehicle
// @Summary      车辆展示
// @Description  车辆展示
// @Tags         车辆模块
// @Accept       json
// @Produce      json
// @Param        vehicle   query   request.ListVehicleReq   true  "车辆参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /vehicle/getVehicle [get]
func GetVehicle(c *gin.Context) {
	var req request.ListVehicleReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	vehicle, err := config.DriverClient.ListVehicle(c, &driver.ListVehicleReq{})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "车辆展示成功", vehicle.ListVehicle)
}
